//
//  BLSiteController.m
//  Byelone
//
//  Created by 乔小乔 on 2020/12/14.
//

#import "BLMeetController.h"
#import "BLMeet_Cell_Index.h"
#import "BLMeet_DetailController.h"
#import "BLMeetSiftVC.h"
#import "BLDestineRoomVC.h"
#import "BLDestineController.h"

#import "BLMeetModel.h"
static NSString *reuse_BLMeet_Cell_Index = @"Reuse_BLMeet_Cell_Index";
@interface BLMeetController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableViewList;
@property (strong, nonatomic) IBOutlet UIView *viewHeader;
@property (weak, nonatomic) IBOutlet UIView *viewTop;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutBjBottom;


@property (nonatomic, strong) NSMutableArray *arrayData;

@property (nonatomic, strong) WMPageController *pageController;
@property (nonatomic, assign) NSInteger searchType;
@property (nonatomic, strong) NSDictionary *searchDic;
@property (nonatomic, strong) NSDictionary *searchStrDic;

@property (nonatomic, assign) NSInteger myPage;

@end
#import "WMPageController.h"

@implementation BLMeetController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    self.myPage = 1;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeType) name:Notification_login object:nil];
    self.arrayData = [NSMutableArray array];
    if ([BLUserManager shareUser].userPpu &&(![[BLUserManager shareUser].registerStep isEqualToString:@"1"]&&![[BLUserManager shareUser].registerStep isEqualToString:@"2"])){//已登录默认值0，未登录是2，筛选是1
        self.searchType = 0;
    }else{
        self.searchType = 2;
    }
    [self loadData];
}
- (void)changeType{
    if ([BLUserManager shareUser].userPpu &&(![[BLUserManager shareUser].registerStep isEqualToString:@"1"]&&![[BLUserManager shareUser].registerStep isEqualToString:@"2"])){//已登录默认值0，未登录是2，筛选是1
        self.searchType = 0;
    }else{
        self.searchType = 2;
    }
    [self refreshData];
}
- (void)createUI{
    self.viewTop.layer.cornerRadius = 14;
    self.viewTop.layer.masksToBounds = YES;
    self.viewTop.layer.borderColor = [UIColor chColorWithHexString:@"#6C72F5"].CGColor;
    self.viewTop.layer.borderWidth = 8;
    
    self.view.backgroundColor = BL_COLOR_LINEBACKGROUND;
    
    self.tableViewList.tableHeaderView = self.viewHeader;
}
- (void)refreshData{
    self.myPage = 1;
    [self loadData];
}
- (void)loadData{
    NSDictionary *dict = @{@"searchType":@(self.searchType),@"pageNo":@(self.myPage),@"pageSize":@(10)};
    if (self.searchDic.allKeys.count>0) {
        NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithDictionary:self.searchDic];
        [muDict setValue:@(1) forKey:@"searchType"];
        dict = muDict;
    }
    [BLHttpTool getHttpRequest:BL_GC_SEARCH param:dict finish:^(NSURLSessionDataTask * _Nonnull data, NSDictionary * _Nonnull obj) {
        [self.tableViewList.mj_header endRefreshing];
        [self.tableViewList.mj_footer endRefreshing];
        if (obj) {
            if (self.myPage == 1) {
                [self.arrayData removeAllObjects];
            }
            
            BLMeetModel *dataModel = [BLMeetModel mj_objectWithKeyValues:obj[@"content"]];
            [self.arrayData addObjectsFromArray:dataModel.list];
            [self.tableViewList reloadData];
            if (dataModel.list.count<10) {
                self.tableViewList.mj_footer = nil;
            }else if(!self.tableViewList.mj_footer){
                self.tableViewList.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
            }
            self.myPage++;
        }
    } failedBlock:^(id  _Nonnull data, NSError * _Nonnull error) {
        [self.tableViewList.mj_header endRefreshing];
        [self.tableViewList.mj_footer endRefreshing];
    }];
}
- (IBAction)clickSift:(id)sender {
    BLMeetSiftVC *vc = [[BLMeetSiftVC alloc]init];
    if (self.searchDic.allKeys.count>0) {
        vc.siftInfo = [BLMeetSiftInfo mj_objectWithKeyValues:self.searchDic];
        vc.searchStrDic = self.searchStrDic;
    }
    vc.myBlock = ^(NSDictionary *dict, NSDictionary *dictStr) {
        self.searchDic = nil;
        if (dict.allKeys.count>0) {
            self.searchDic = dict;
        }
        self.searchStrDic = dictStr;
        [self.tableViewList.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BLMeetItem *item = self.arrayData[indexPath.row];
    BLMeet_Cell_Index *cell = [tableView dequeueReusableCellWithIdentifier:reuse_BLMeet_Cell_Index forIndexPath:indexPath];
    cell.myBlock = ^(NSInteger index) {
//        101,102,103
        if (index == 103) {
            BLDestineController * des = [[BLDestineController alloc]init];
            [self.navigationController pushViewController:des animated:YES];
        }
    };
    [cell reloadWithData:item];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (BL_WINDOW_WIDTH-30)*200/345+117;
//    return UITableViewAutomaticDimension;;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BLMeetItem *item = self.arrayData[indexPath.row];
    BLMeet_DetailController *vc = [[BLMeet_DetailController alloc]init];
    vc.searchUserId = item.userHomeBaseVo.userId.integerValue;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y<=0) {
        self.layoutBjBottom.constant = scrollView.contentOffset.y;
    }    
}
#pragma mark - set&get
- (UITableView *)tableViewList{
    if (!_tableViewList) {
        _tableViewList = [[UITableView alloc]initWithFrame:CGRectMake(0, BL_NAVANDSTATUS_HEIGHT, BL_WINDOW_WIDTH, BL_TABCONTENT_HEIGHT)];
        [self.view addSubview:_tableViewList];
        _tableViewList.backgroundColor = [UIColor clearColor];
        _tableViewList.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableViewList.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        _tableViewList.delegate = self;
        _tableViewList.dataSource = self;
        if (@available(iOS 11.0, *)) {
            _tableViewList.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        [_tableViewList registerNib:[UINib nibWithNibName:@"BLMeet_Cell_Index" bundle:nil] forCellReuseIdentifier:reuse_BLMeet_Cell_Index];
    }
    return _tableViewList;
}

@end
